package com.minis.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
    protected List<String> beanNames = new ArrayList<>();
    protected Map<String, Object> singletons = new HashMap<>();

    @Override
    public void registerSingleton(String beanName, Object obj) {
        synchronized(this.singletons) {
            singletons.put(beanName, obj);
            beanNames.add(beanName);
        }
    }

    @Override
    public boolean containsSingleton(String beanName) {
        return singletons.containsKey(beanName);
    }

    @Override
    public Object getSingleton(String beanName) {
        return singletons.get(beanName);
    }

    @Override
    public String[] getSingletonNames() {
        return (String[]) beanNames.toArray();
    }

    protected void removeSingleton(String beanName) {
        synchronized(this.singletons) {
            singletons.remove(beanName);
            beanNames.remove(beanName);
        }
    }
}
